package com.zsj.comminlibrary.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * Created by 杰 on 2018/6/22.
 */

public class ZipUtils {
    private static WeakReference<byte[]> buffer;

    public ZipUtils() {
    }

    private static byte[] getBuffer() {
        byte[] var0 = buffer == null?null:(byte[])buffer.get();
        if(var0 == null) {
            var0 = new byte[4096];
        }

        if(buffer == null || var0 != buffer.get()) {
            buffer = new WeakReference(var0);
        }

        return var0;
    }

    public static void zipDirectory(String var0, String var1) throws IOException {
        File var2 = new File(var0);
        if(var2.exists()) {
            var2.delete();
        }

        File var3 = new File(var1);
        if(var3.exists()) {
            File[] var4 = var3.listFiles();
            if(var4 != null) {
                String[] var5 = new String[var4.length];

                for(int var6 = 0; var6 < var4.length; ++var6) {
                    var5[var6] = var1 + "/" + var4[var6].getName();
                }

                zip(var0, var5);
            }

        }
    }

    public static void zip(String var0, String... var1) throws IOException {
        ZipOutputStream var2 = new ZipOutputStream(new FileOutputStream(var0));

        for(int var3 = 0; var3 < var1.length; ++var3) {
            zip(var2, new File(var1[var3]), "");
        }

        var2.close();
    }

    public static void zip(String var0, File var1) throws IOException {
        ZipOutputStream var2 = new ZipOutputStream(new FileOutputStream(var0));
        zip(var2, var1, "");
        var2.close();
    }

    private static void zip(ZipOutputStream var0, File var1, String var2) throws IOException {
        ZipEntry var3;
        if(var1.isDirectory()) {
            var2 = var2.length() == 0?var1.getName():var2 + "/" + var1.getName();
            var3 = new ZipEntry(var2 + "/");
            var3.setTime(var1.lastModified());
            var0.putNextEntry(var3);
            File[] var4 = var1.listFiles();

            for(int var5 = 0; var5 < var4.length; ++var5) {
                zip(var0, var4[var5], var2);
            }
        } else {
            var2 = var2.length() == 0?var1.getName():var2 + "/" + var1.getName();
            var3 = new ZipEntry(var2);
            var3.setTime(var1.lastModified());
            var0.putNextEntry(var3);
            FileInputStream var7 = new FileInputStream(var1);
            byte[] var8 = getBuffer();

            int var6;
            while((var6 = var7.read(var8)) != -1) {
                var0.write(var8, 0, var6);
            }

            var7.close();
        }

    }

    public static void zip(InputStream var0, OutputStream var1) throws IOException {
        ZipOutputStream var2 = new ZipOutputStream(var1);
        ZipEntry var3 = new ZipEntry("/");
        var2.putNextEntry(var3);
        byte[] var4 = getBuffer();

        int var5;
        while((var5 = var0.read(var4)) != -1) {
            var2.write(var4, 0, var5);
        }

        var0.close();
        var2.close();
        var1.close();
    }

    public static void unzip(InputStream var0, OutputStream var1) throws IOException {
        ZipInputStream var2 = new ZipInputStream(var0);
        ZipEntry var3 = var2.getNextEntry();
        if(var3 != null) {
            byte[] var4 = getBuffer();

            int var5;
            while((var5 = var2.read(var4)) != -1) {
                var1.write(var4, 0, var5);
            }

            var2.close();
            var0.close();
            var1.close();
        }
    }

    public static void unzip(String var0, String var1) throws IOException {
        ZipInputStream var2 = new ZipInputStream(new FileInputStream(var0));

        while(true) {
            ZipEntry var3;
            while((var3 = var2.getNextEntry()) != null) {
                if(var3.isDirectory()) {
                    String var8 = var3.getName();
                    var8 = var8.substring(0, var8.length() - 1);
                    File var9 = new File(var1 + File.separator + var8);
                    var9.mkdirs();
                    var9.setLastModified(var3.getTime());
                } else {
                    File var4 = new File(var1 + File.separator + var3.getName());
                    var4.getParentFile().mkdirs();
                    FileOutputStream var5 = new FileOutputStream(var4);
                    byte[] var6 = getBuffer();

                    int var7;
                    while((var7 = var2.read(var6)) != -1) {
                        var5.write(var6, 0, var7);
                    }

                    var5.close();
                    var4.setLastModified(var3.getTime());
                }
            }

            var2.close();
            return;
        }
    }

    public static Collection<String> list(String var0, String var1) throws IOException {
        ZipInputStream var2 = new ZipInputStream(new FileInputStream(var0));
        boolean var4 = var1 == null;
        boolean var5 = var1 == null || var1.length() == 0 || "/".equals(var1);
        if(!var5) {
            var1 = var1 + "/";
        }

        LinkedHashSet var6 = new LinkedHashSet();

        ZipEntry var3;
        while((var3 = var2.getNextEntry()) != null) {
            String var7 = var3.getName();
            if(var4) {
                var6.add(var7);
            } else if(var5) {
                if(!var7.equals("/")) {
                    if(var7.indexOf(47) < 0) {
                        var6.add(var7);
                    } else {
                        var6.add(var7.substring(0, var7.indexOf(47)));
                    }
                }
            } else if(var7.startsWith(var1)) {
                int var8 = var7.indexOf(47, var1.length());
                if(var8 > 0) {
                    var6.add(var7.substring(var1.length(), var8));
                } else {
                    var6.add(var7.substring(var1.length()));
                }
            }
        }

        return var6;
    }
}
